package com.ybxx.fees.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.FeesItem;
import com.ybxx.core.model.BaseModel;

@Entity
@Table(name="tbl_fees_wsf_record")
public class WsfRecord extends BaseModel {
	@Id
	private String wsfrcode;
	@ManyToOne(targetEntity=Records.class,fetch=FetchType.LAZY)
	@JoinColumn(name="frcode",referencedColumnName="frcode") 
	private Records records;
	private String receivable;
	@OneToMany(targetEntity=WsfRecordItem.class,cascade={CascadeType.ALL},mappedBy="wsfRecord",fetch = FetchType.LAZY)
	private Set<WsfRecordItem> items=new HashSet<WsfRecordItem>();
	@Transient
	private List<WsfRecordItem> itemsList=new ArrayList<WsfRecordItem>();
	public String getWsfrcode() {
		return wsfrcode;
	}
	public void setWsfrcode(String wsfrcode) {
		this.wsfrcode = wsfrcode;
	}
	public Records getRecords() {
		return records;
	}
	public void setRecords(Records records) {
		this.records = records;
	}
	public String getReceivable() {
		return receivable;
	}
	public void setReceivable(String receivable) {
		if(StringUtils.isNotEmpty(receivable)){
			this.receivable = receivable;
		}
	}
	public Set<WsfRecordItem> getItems() {
		return items;
	}
	public void setItems(Set<WsfRecordItem> items) {
		this.items = items;
	}
	public List<WsfRecordItem> getItemsList() {
		if(itemsList==null || itemsList.size()<=0){
			this.itemsList.addAll(items);
		}else{
			this.items.addAll(itemsList);
		}
		if(this.itemsList!=null){
			Collections.sort(this.itemsList, new Comparator<WsfRecordItem>(){

				@Override
				public int compare(WsfRecordItem o1, WsfRecordItem o2) {
					// TODO Auto-generated method stub
					return o1.getPrice().compareTo(o2.getPrice());
				}});
		}
		return itemsList;
	}
	
}
